require "pp"
require "rubygems"
#require 'jcode'
#$KCODE='u'
require 'iconv'

scheduler = Rufus::Scheduler.start_new
#require "rfetion"
require 'net/http'
require 'rubygems'
require 'logger'
scheduler.every '1000s' do
  xing = %W(zhao qian sun li zhou wu zheng wang feng chen chu wei jang
           shen han yang zhu qin you xu he lu shi zhang kou cao yan hua
            jin lou tao jiang qi xie zou yu bai shui dou zhang yun su pan ge xi fan peng lang lu wei
            chang ma miao ren feng hau fang yu ren yuan liu  feng bao shi tang fei lian cen
            xue qing ma ji xi min lan ruan du liang dong zhu xiang qu shu ji xiong pang mao song tan dai
            cheng  fu ji zang ming bei mi di yu mao qi wang zhan shao yao yin xiao mu he huang ping meng
            gu bu yuang yu wu lei he ni tang jia lu lou wei jiang tong yan guo mei sheng lin diao zhong xu qiu
            luo gao xia cai tian fan hu lin huo yu wan zhi ke zan guan lu mo jing fang qiu miao gan xie ying zong
            ding xuan ben deng yu shan hang hong bao zhu zuo shi cui ji niu gong cheng ji xing hua pei lu
            lu rong weng xun yang yu hui  zhen qu jia feng ren yi chu jin ji bing mi song jing duan fu wu jiao ba gong
            nu kui shan gu che hou mi peng quan xi ban yang qiu zhong yi gong ning qiu luan bao gan tao li rong zu
            wu fu liu jing zhan shu long ye xing si shao gao li ji bo yin su bai huai pu tai cong e suo xian ji lian
            lan zhuo li tu men chi qiao yin yu xu nai cang shuang  ji ju heng bu zhong yu liao ge shen yi gu xiao rong yu ai
            huan xi ru lian nu chong yan qu chai wen shen dang zhai tan gong lao pang ji shen fu du ran zai li yong xi qu sang
            gui pu shou tong bian hui yan ji jia pu shang nong wen bie zhong yan tong yang nian ai da qiao ha mo shang
            bo mou shang qin you hou kuang kang gou shuai yue sheng wei hai gui jin chu yan fa ru yan tu qin gong
            huan yi gai lu quan zhu you hong jing hou zha xiang kuai guan chao feng xu ju yang nie sha nu zeng kong
            rao jian na kan xin zi leng rong ao gou chao nie she gong shi long kui  yue wei li wo shu ou dong que
            lu guang kou wen gou kuang hong man geng du )
  ming = ('a'..'z').to_a 
  other = ('0'..'9').to_a
  UserMailer.deliver_adv("#{xing.rand.strip}#{ming.rand}@126.com")
  email = Sendemail.find(:first,:order => 'status asc')
  UserMailer.deliver_adv(email.email)
  email.status = email.status.to_i + 1
  email.save
   
end

scheduler.every '3600s' do
  @cnv =  Iconv.new("UTF-8//IGNORE","GBK//IGNORE")
  ##############################鉴别鉴定 http://www.woiyu.com/jade/appraise/index.html
  if Time.now.hour == 15
    @body =  Net::HTTP.get(URI.parse("http://www.woiyu.com/jade/appraise/index.html"))
    @titles = @body.scan(/<a href='(.*?)' class='preview'>/).map{|x| x.first}
    @titles.each do |url|
      article = Net::HTTP.get(URI.parse(url))
      art = Article.new
      art.title =  article.scan(/<h2>(.*?)<\/h2>/).to_s

      art.title =  @cnv.iconv(art.title)
      art.subhead =  @cnv.iconv(article.scan(/<h2>(.*?)<\/h2>/).to_s)
      art.from =  "爱玉网"
      art.author =  "爱玉网"
      art.summary =  @cnv.iconv(article.scan(/<div class="intro">(.*?)<\/div>/).to_s)
 
      art.body =  @cnv.iconv(article.scan(/(<div class="content">)([\s\S\w\W.]*)(<\/div>(.*?)<!-- \/content -->)/).to_s)
      art.status =  "新闻"
      art.user =  User.first
      art.category =  Category.find_by_name('品牌时尚')
      art.save
    end
  end
  ####################翡翠常识  http://www.woiyu.com/jade/commonsense/index.html
  if Time.now.hour == 2
    @body =  Net::HTTP.get(URI.parse("http://www.woiyu.com/jade/commonsense/index.html"))
    @titles = @body.scan(/<a href='(.*?)' class='preview'>/).map{|x| x.first}
    @titles.each do |url|
      article = Net::HTTP.get(URI.parse(url))
      art = Article.new
      art.title =  @cnv.iconv(article.scan(/<h2>(.*?)<\/h2>/).to_s)
      art.subhead =  @cnv.iconv(article.scan(/<h2>(.*?)<\/h2>/).to_s)
      art.summary =  @cnv.iconv(article.scan(/<div class="intro">(.*?)<\/div>/).to_s)
      art.body =  @cnv.iconv(article.scan(/(<div class="content">)([\s\S\w\W.]*)(<\/div>(.*?)<!-- \/content -->)/).to_s)
      art.from =  "爱玉网"
      art.author =  "爱玉网"
      art.status =  "新闻"
      art.user =  User.first
      art.category =  Category.find_by_name('常识寓意')
      art.save
    end
  end
  ####################雕刻镶嵌  http://www.woiyu.com/jade/SculptureMosaic/index.html
  if Time.now.hour == 3
    @body =  Net::HTTP.get(URI.parse("http://www.woiyu.com/jade/SculptureMosaic/index.html"))
    @titles = @body.scan(/<a href='(.*?)' class='preview'>/).map{|x| x.first}
    @titles.each do |url|
      article = Net::HTTP.get(URI.parse(url))
      art = Article.new
      art.title =  @cnv.iconv(article.scan(/<h2>(.*?)<\/h2>/).to_s)
      art.subhead =  @cnv.iconv(article.scan(/<h2>(.*?)<\/h2>/).to_s)
      art.summary =  @cnv.iconv(article.scan(/<div class="intro">(.*?)<\/div>/).to_s)
      art.body =  @cnv.iconv(article.scan(/(<div class="content">)([\s\S\w\W.]*)(<\/div>(.*?)<!-- \/content -->)/).to_s)
      art.from =  "爱玉网"
      art.author =  "爱玉网"
      art.status =  "新闻"
      art.user =  User.first
      art.category =  Category.find_by_name('常识寓意')
      art.save
    end
  end
  ####################收藏投资  http://www.woiyu.com/jade/favoritesinvestment/index.html
  if Time.now.hour == 4
    @body =  Net::HTTP.get(URI.parse("http://www.woiyu.com/jade/favoritesinvestment/index.html"))
    @titles = @body.scan(/<a href='(.*?)' class='preview'>/).map{|x| x.first}
    @titles.each do |url|
      article = Net::HTTP.get(URI.parse(url))
      art = Article.new
      art.title =  @cnv.iconv(article.scan(/<h2>(.*?)<\/h2>/).to_s)
      art.subhead =  @cnv.iconv(article.scan(/<h2>(.*?)<\/h2>/).to_s)
      art.summary =  @cnv.iconv(article.scan(/<div class="intro">(.*?)<\/div>/).to_s)
      art.body =  @cnv.iconv(article.scan(/(<div class="content">)([\s\S\w\W.]*)(<\/div>(.*?)<!-- \/content -->)/).to_s)
      art.from =  "爱玉网"
      art.author =  "爱玉网"
      art.status =  "新闻"
      art.user =  User.first
      art.category =  Category.find_by_name('收藏保值')
      art.save
    end
  end
  ####################翡翠故事  http://www.woiyu.com/jade/favoritesinvestment/index.html
  if Time.now.hour == 5
    @body =  Net::HTTP.get(URI.parse("http://www.woiyu.com/jade/favoritesinvestment/index.html"))
    @titles = @body.scan(/<a href='(.*?)' class='preview'>/).map{|x| x.first}
    @titles.each do |url|
      article = Net::HTTP.get(URI.parse(url))
      art = Article.new
      art.title =  @cnv.iconv(article.scan(/<h2>(.*?)<\/h2>/).to_s)
      art.subhead =  @cnv.iconv(article.scan(/<h2>(.*?)<\/h2>/).to_s)
      art.summary =  @cnv.iconv(article.scan(/<div class="intro">(.*?)<\/div>/).to_s)
      art.body =  @cnv.iconv(article.scan(/(<div class="content">)([\s\S\w\W.]*)(<\/div>(.*?)<!-- \/content -->)/).to_s)
      art.from =  "爱玉网"
      art.author =  "爱玉网"
      art.status =  "新闻"
      art.user =  User.first
      art.category =  Category.find_by_name('常识寓意')
      art.save
    end
  end
  ####################翡翠赌石  http://www.woiyu.com/jade/gamblingstone/index.html
  if Time.now.hour == 6
    @body =  Net::HTTP.get(URI.parse("http://www.woiyu.com/jade/gamblingstone/index.html"))
    @titles = @body.scan(/<a href='(.*?)' class='preview'>/).map{|x| x.first}
    @titles.each do |url|
      article = Net::HTTP.get(URI.parse(url))
      art = Article.new
      art.title =  @cnv.iconv(article.scan(/<h2>(.*?)<\/h2>/).to_s)
      art.subhead =  @cnv.iconv(article.scan(/<h2>(.*?)<\/h2>/).to_s)
      art.summary =  @cnv.iconv(article.scan(/<div class="intro">(.*?)<\/div>/).to_s)
      art.body =  @cnv.iconv(article.scan(/(<div class="content">)([\s\S\w\W.]*)(<\/div>(.*?)<!-- \/content -->)/).to_s)
      art.from =  "爱玉网"
      art.author =  "爱玉网"
      art.status =  "新闻"
      art.user =  User.first
      art.category =  Category.find_by_name('矿山赌石')
      art.save
    end
  end
  ####################翡翠名家  http://www.woiyu.com/jade/master/index.html
  if Time.now.hour == 7
    @body =  Net::HTTP.get(URI.parse("http://www.woiyu.com/jade/master/index.html"))
    @titles = @body.scan(/<a href='(.*?)' class='preview'>/).map{|x| x.first}
    @titles.each do |url|
      article = Net::HTTP.get(URI.parse(url))
      art = Article.new
      art.title =  @cnv.iconv(article.scan(/<h2>(.*?)<\/h2>/).to_s)
      art.subhead =  @cnv.iconv(article.scan(/<h2>(.*?)<\/h2>/).to_s)
      art.summary =  @cnv.iconv(article.scan(/<div class="intro">(.*?)<\/div>/).to_s)
      art.body =  @cnv.iconv(article.scan(/(<div class="content">)([\s\S\w\W.]*)(<\/div>(.*?)<!-- \/content -->)/).to_s)
      art.from =  "爱玉网"
      art.author =  "爱玉网"
      art.status =  "新闻"
      art.user =  User.first
      art.category =  Category.find_by_name('品牌时尚')
      art.save
    end
  end
  ####################行业资讯  http://www.woiyu.com/jade/xingyezixun/index.html 品牌时尚
  if Time.now.hour == 8
    @body =  Net::HTTP.get(URI.parse("http://www.woiyu.com/jade/xingyezixun/index.html"))
    @titles = @body.scan(/<a href='(.*?)' class='preview'>/).map{|x| x.first}
    @titles.each do |url|
      article = Net::HTTP.get(URI.parse(url))
      art = Article.new
      art.title =  @cnv.iconv(article.scan(/<h2>(.*?)<\/h2>/).to_s)
      art.subhead =  @cnv.iconv(article.scan(/<h2>(.*?)<\/h2>/).to_s)
      art.summary =  @cnv.iconv(article.scan(/<div class="intro">(.*?)<\/div>/).to_s)
      art.body =  @cnv.iconv(article.scan(/(<div class="content">)([\s\S\w\W.]*)(<\/div>(.*?)<!-- \/content -->)/).to_s)
      art.from =  "爱玉网"
      art.author =  "爱玉网"
      art.status =  "新闻"
      art.user =  User.first
      art.category =  Category.find_by_name('品牌时尚')
      art.save
    end
  end
  ############################百度
  #  if Time.now.hour == 8 || Time.now.hour == 16  || Time.now.hour == 20
  #    agent = Mechanize.new
  #    page = agent.get("http://news.baidu.com/ns?word=%F4%E4%B4%E4&ie=gb2312&bs=%F4%E4%B4%E4&sr=0&cl=2&rn=20&tn=newsA&ct=0&clk=sortbytime")
  #    body = page.search("//div[@id='r']/table")
  #    cnv =  Iconv.new("UTF-8","UTF-8")
  #    @title =  cnv.iconv(body[0].at('b'))
  #    body[0].at('font').remove
  #    @body =  cnv.iconv(body[0].at('font'))
  #    art = Article.new
  #    art.title =  @title
  #    art.subhead =  @title
  #    art.from =  "爱玉网"
  #    art.author =  "爱玉网"
  #    art.summary = @title
  #    art.body =  @body
  #    art.status =  "新闻"
  #    art.user =  User.first
  #    art.category =  Category.find_by_name('品牌时尚')
  #    art.save
  #  end
  Rails.logger.info('success')
end


